package com.itheima.tcp.fileupload.demo1;

import java.io.*;
import java.net.Socket;

//发送端
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建文件
        File file = new File("E:\\itcast\\mn.jpg");
        String host = "127.0.0.1";
        int post = 10000;
        Socket socket = null;
        FileInputStream fis = null;
        try {
            //创建客户端socket对象
            socket = new Socket(host,post);

            //通过socket对象获取输出流对象
            OutputStream outputStream = socket.getOutputStream();
            //创建本地文件输入流读取本地文件
            fis = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int b;
            while ((b = fis.read(bytes)) != -1){
                outputStream.write(bytes,0,b);
            }
            fis.close();
            socket.shutdownOutput();

            //创建输入流对象
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String msg;
            while ((msg = br.readLine()) != null){
                System.out.println(msg);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                fis.close();
            }
            socket.close();
        }

    }
}
